package com.lagou.sqlSession;

import com.lagou.config.XMLConfigBuilder;
import com.lagou.config.XMLConfigBuilderInsert;
import com.lagou.pojo.Configuration;
import com.lagou.utils.SqlTypeEnum;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream in, byte sqlTypeEnum) throws DocumentException, PropertyVetoException {
        //当sql类型为查询时
        if (SqlTypeEnum.SELECT.getCode().equals(sqlTypeEnum)) {
            // 第一：使用dom4j解析配置文件，将解析出来的内容封装到Configuration中
            XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
            Configuration configuration = xmlConfigBuilder.parseConfig(in);


            // 第二：创建sqlSessionFactory对象：工厂类：生产sqlSession:会话对象
            DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);

            return defaultSqlSessionFactory;
        }
        //当操作为数据修改时
        // 第一：使用dom4j解析配置文件，将解析出来的内容封装到Configuration中
        XMLConfigBuilderInsert xmlConfigBuilder = new XMLConfigBuilderInsert();
        Configuration configuration = xmlConfigBuilder.parseConfig(in);
        // 第二：创建sqlSessionFactory对象：工厂类：生产sqlSession:会话对象
        DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        return defaultSqlSessionFactory;
    }


}
